﻿using AutoMapper;
using MediatR;
using RedDragon.Common.Domain.Enterprise;
using RedDragon.Common.Domain.MaterialManagements;
using RedDragon.Common.Domain.MaterialPlanning;
using RedDragon.Common.Dtos;
using RedDragon.Common.Infrastructure.Interface;
using RedDragon.ERP.ErroCodeEnum;
using SqlSugar;
using T.Common.Domain.Common;

namespace RedDragon.ERP.API.Application.Commands.MaterialPlanning
{
    /// <summary>
    /// 物料需求计划列表Handler
    /// </summary>
    public class SearchPlanHandler : IRequestHandler<SearchPlanCommand, Result<List<PlanDto>>>
    {
        private readonly ISqlSugarClient _db;
        /// <summary>
        /// mapper
        /// </summary>
        private readonly IMapper mapper;
        /// <summary>
        /// 物料需求计划表仓储
        /// </summary>
        public readonly IBaseRepository<Plan> planrepository;
        /// <summary>
        /// 部门表仓储
        /// </summary>
        public readonly IBaseRepository<Departments> departmentsrepository;
        /// <summary>
        /// 日志
        /// </summary>
        private readonly ILogger<SearchPlanHandler> logger;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mapper"></param>
        /// <param name="planrepository"></param>
        /// <param name="departmentsrepository"></param>
        /// <param name="logger"></param>
        /// <param name="db"></param>
        public SearchPlanHandler(IMapper mapper, IBaseRepository<Plan> planrepository, IBaseRepository<Departments> departmentsrepository, ILogger<SearchPlanHandler> logger, ISqlSugarClient db)
        {
            this.mapper = mapper;
            this.planrepository = planrepository;
            this.departmentsrepository = departmentsrepository;
            this.logger = logger;
            _db = db;
        }
        /// <summary>
        /// 物料需求计划列表
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result<List<PlanDto>>> Handle(SearchPlanCommand request, CancellationToken cancellationToken)
        {
            var result = new Result<List<PlanDto>>() { Code = (int)CommonErrorCode.Success };
            try
            {
                var query = await _db.Queryable<Plan, Departments>((a, b) => new JoinQueryInfos(
                    JoinType.Inner, a.DepartmentId == b.DepartmentId
                    ))
                    .Select((a, b) => new PlanDto()
                    {
                        PlanId = a.PlanId,
                        PlanCode=a.PlanCode,
                        PlanName=a.PlanName,
                        DepartmentId=a.DepartmentId,
                        DepartmentName=b.DepartmentName,
                        SingleMaker=a.SingleMaker,
                        IsStatus = a.IsStatus,
                        IsDel = a.IsDel,
                        AuditStatus = a.AuditStatus,
                        CreatedOwner = a.CreatedOwner,
                        CreatedTime = a.CreatedTime
                    }).Where(a => a.IsDel == 1).ToListAsync();

                result.Data = query;
                result.Msg = "物料需求计划显示查询成功";
            }
            catch (Exception ex)
            {
                result.Msg = "物料需求计划显示查询失败" + ex.Message;
                logger.LogError("物料需求计划显示查询失败" + ex.Message);
                throw;
            }
            return result;
        }
    }
}
